package com.zoe.condition;

import com.zoe.bean.RainBow;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

/**
 * @Author Zoe
 * @Date: 2020/11/17
 * @Description
 */
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
    /**
     *
     * @param importingClassMetadata 当前类的注解信息
     * @param registry 注册类
     *                 把所有需要添加到容器中的bean；调用
     *                 BeanDefinitionRegistry.registry.registerBeanDefinition();添加到容器中
     */
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        boolean yellow = registry.containsBeanDefinition("com.zoe.bean.Yellow");
        boolean blue = registry.containsBeanDefinition("com.zoe.bean.Blue");
        // 如果容器中有改组件那么就往容器中注册组件
        if (yellow && blue){
            // 指定注解的类名
            RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(RainBow.class);
            registry.registerBeanDefinition("RainBow",rootBeanDefinition);
        }

    }
}
